package cn.stylefeng.guns.modular.basic.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.basic.entity.BasicCustomer;
import cn.stylefeng.guns.modular.basic.entity.BasicOperate;
import cn.stylefeng.guns.modular.basic.model.params.BasicCustomerParam;
import cn.stylefeng.guns.modular.basic.service.BasicCustomerService;
import cn.stylefeng.guns.modular.basic.service.BasicOperateService;
import cn.stylefeng.guns.sys.modular.system.entity.User;
import cn.stylefeng.guns.sys.modular.system.service.UserService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;


/**
 * 客户表控制器
 */
@Controller
@RequestMapping("/basicCustomer")
public class BasicCustomerController extends BaseController {

    private String PREFIX = "/basic/basicCustomer";

    @Autowired
    private BasicCustomerService basicCustomerService;

    @Autowired
    private BasicOperateService basicOperateService;

    @Autowired
    private UserService userService;

    /**
     * 跳转到主页面
     */
    @RequestMapping("")
    public String index(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicCustomer.html";
    }

    /**
     * 新增页面
     */
    @RequestMapping("/add")
    public String add(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicCustomer_add.html";
    }

    /**
     * 编辑页面
     *
     * @author zhucx
     * @Date 2019-06-21
     */
    @RequestMapping("/edit")
    public String edit(Model model) {
    	User user = userService.getcurrentUser();
    	Long deptId = user.getDeptId();
    	Long deliverId = user.getDeliverId();
    	
    	//查询可选择分公司
    	model.addAttribute("basicOperatelist", basicOperateService.findAvailableList(deptId,deliverId,user.getOperateId()));
        return PREFIX + "/basicCustomer_edit.html";
    }

    /**
     * 新增接口
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(BasicCustomerParam basicCustomerParam) {
    	Object customerOperateId = basicCustomerParam.getCustomerOperateId();
    	if (customerOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(customerOperateId.toString()));
    		basicCustomerParam.setCustomerOperateName(Operate.getOperateName());
    		basicCustomerParam.setDeptId(Operate.getDeptId());
		}
        
        //set 交付id
    	Long DeliverId = userService.getDeliverId();
    	if (DeliverId!=null) {
    		basicCustomerParam.setDeliverId(DeliverId);
		}
    	basicCustomerParam.setStatus("1");
        this.basicCustomerService.add(basicCustomerParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(BasicCustomerParam basicCustomerParam) {
    	Object customerOperateId = basicCustomerParam.getCustomerOperateId();
    	if (customerOperateId!=null) {
    		BasicOperate Operate = basicOperateService.getById(Integer.parseInt(customerOperateId.toString()));
    		basicCustomerParam.setCustomerOperateName(Operate.getOperateName());
    		basicCustomerParam.setDeptId(Operate.getDeptId());
		}
        this.basicCustomerService.update(basicCustomerParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(BasicCustomerParam basicCustomerParam) {
        this.basicCustomerService.delete(basicCustomerParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(BasicCustomerParam basicCustomerParam) {
        BasicCustomer detail = this.basicCustomerService.getById(basicCustomerParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(BasicCustomerParam basicCustomerParam,String condition) {
    	User user = userService.getcurrentUser();
    	basicCustomerParam.setDeliverId(user.getDeliverId());
    	basicCustomerParam.setDeptId(user.getDeptId());
        return this.basicCustomerService.findPageBySpec(basicCustomerParam,condition,user.getOperateId());
    }

}


